<template>
  <div class="box">
    <!-- 头部 -->
    <div class="header">
      <van-icon name="wap-home-o" @click="goHome" />
      <p class="title">值得买</p>
      <div>
        <van-icon name="search" class="search" @click="goSearch" />
        <van-icon name="shopping-cart-o" @click="goShopCart" />
      </div>
    </div>
    <!-- 轮播  -->
    <div class="swipter">
      <!-- 背景图 -->
      <img class="backgroundImg" src="https://yanxuan.nosdn.127.net/a93a392fb8006ba26dea38477979b7b4.jpg?imageView" />
      <!-- 轮播图 -->
      <div class="content">
        <img src="http://yanxuan.nosdn.127.net/0b7676e645253f84be662aacfc051922.png" alt="" />
        <div class="title">严选好物 用心生活</div>
      </div>
      <div class="swipter_my">
        <!--   transform: translateX(48.84210205078125px); -->
        <div class="itemAll" @touchstart="handle" :style="{ transform: `translateX(${swipterx}px)` }">
          <div class="item" v-for="(item, index) in swipterList" :key="index">
            <img :src="item.picUrl" alt="" />
            <div class="text1">{{ item.mainTitle }}</div>
            <div class="text2">{{ item.viceTitle }}</div>
          </div>
        </div>
        <div class="progress">
          <ul>
            <li v-for="(item, index) in 4" :key="index" :class="{ active: isLi == index }"></li>
          </ul>
        </div>
      </div>
    </div>
    <!-- 瀑布流 -->
    <Waterfall></Waterfall>
  </div>
</template>

<script>
import Vue from "vue";
import { Grid, GridItem, Swipe, SwipeItem } from "vant";
Vue.use(Swipe);
Vue.use(SwipeItem);
Vue.use(Grid);
Vue.use(GridItem);
// 引入懒加载
import { Lazyload } from "vant";
Vue.use(Lazyload);
// 引入瀑布流
import Waterfall from "./waterfall/index.vue";
import { mapState } from "vuex";
export default {
  data() {
    return {
      swipterx: 0,
      isLi: 0,
    };
  },
  methods: {
    // 轮播图移动距离
    handle(event) {
      let start = event.changedTouches[0].clientX;
      let swipterx = this.swipterx;
      // 按下事件
      document.ontouchmove = (event) => {
        let movex = event.changedTouches[0].clientX;

        this.swipterx = swipterx - (start - movex);
        // Math.trunc(-this.swipterx / 100)

        this.isLi = Math.trunc(-this.swipterx / 90);

        if (this.isLi < 0) {
          this.isLi = 0;
        }
        if (this.isLi > 3) {
          this.isLi = 3;
        }
      };
      document.ontouchend = (event) => {
        if (this.swipterx > 0) {
          this.swipterx = 0;
        }
        if (this.swipterx < -290) {
          this.swipterx = -290;
        }

        // 鼠标抬起时，销毁移动事件和鼠标抬起事件
        document.ontouchmove = null;
        document.onmouseup = null;
      };
    },
    // 发送请求获取轮播数据
    getSwipter() {
      this.$store.dispatch("worth/getSwipter");
    },
    // 去首页
    goHome() {
      this.$router.push({ name: "home", params: { index: 0 } });
    },
    // 去购物车
    goShopCart() {
      // {name:'shopcart',params:{index:0}}
      this.$router.push({ name: "shopcart", params: { index: 2 } });
    },
    // 去搜索
    goSearch() {
      this.$router.push("/innerSearch");
    },
  },
  computed: {
    ...mapState({
      swipterList: (state) => {
        return state.worth.swipterList;
      },
    }),
  },
  components: {
    Waterfall,
  },
  mounted() {
    this.getSwipter();
  },
};
</script>

<style lang="less" scoped>
.box {
  width: 100%;
  height: 100%;
  overflow-y: auto;
  background-color: #eee;
  .header {
    position: fixed;
    height: 50px;
    width: 100%;
    display: flex;
    background-color: #fafafa;
    align-items: center;
    padding: 0 20px;
    box-sizing: border-box;
    z-index: 99;
    .title {
      flex: 1;
      text-align: center;
      font-size: 18px;
    }
    .search {
      margin-right: 20px;
    }
  }
  .swipter {
    width: 100%;

    position: relative;
    top: 50px;
    .backgroundImg {
      height: 259px;
      width: 375px;
    }
    .content {
      display: flex;
      position: absolute;
      top: 20px;
      left: 3%;
      align-items: flex-end;
      img {
        height: 35px;
        width: 65px;
      }
      .title {
        font-size: 15px;
        color: white;
      }
    }
    .swipter_my {
      width: 94%;
      height: 270px;
      overflow: hidden;
      position: absolute;
      top: 60px;
      left: 3%;
      background-color: white;
      border-radius: 10px;
      .itemAll {
        height: 240px;
        width: 630px;
        display: flex;
        flex-wrap: wrap;
        /*transition: all 0.2s linear;*/
        .item {
          width: 14.2%;
          display: flex;
          flex-direction: column;
          align-items: center;
          margin-top: 10px;
          img {
            height: 50px;
            width: 50px;
            margin: 5px 0;
            border-radius: 50%;
          }
          .text1 {
            width: 84px;
            height: 15px;
            font-size: 16px;
            margin: 5px 0;
          }
          .text2 {
            width: 84px;
            height: 15px;
            font-size: 14px;
          }
        }
      }
      .progress {
        height: 20px;
        width: 100%;
        display: flex;
        justify-content: center;
        margin-top: 15px;
        ul {
          display: flex;
          li {
            width: 20px;
            height: 2px;
            background-color: #eeeeee;
          }
          .active {
            background-color: red;
          }
        }
      }
    }
  }
}
</style>
